import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
class EmailService {
    private emailConfig
    /**
      * Load the security configuration.
      * @return  the config
      */
    ConfigObject getSecurityConfig() {
        if (emailConfig == null) {
            GroovyClassLoader classLoader = new GroovyClassLoader(getClass().getClassLoader())
            emailConfig = new ConfigSlurper().parse(classLoader.loadClass('EmailConfig'))
        }
        return emailConfig
    }
    boolean transactional = true

    //FIXME new thread
    def send(email) {
        def config = getSecurityConfig();
        if (config.email.enabled) {
            def el = new SimpleEmail();
            
            el.setSslSmtpPort("" + config.email.SslSmtpPort);
            el.setTLS(config.email.TLS);
            el.setHostName(config.email.SmtpHost);
            //FIXME
            el.setAuthentication(config.email.authName, config.email.authPass);
            el.setFrom(config.email.from);
            el.addTo(email.to);
            el.setSubject(email.subject);
            el.setMsg(email.text);
            el.send();
        }
    }
}
